package model

import (
	"context"
	"github.com/zeromicro/go-zero/core/stores/mon"
	"go.mongodb.org/mongo-driver/bson"
)

var _ DockerRunModel = (*customDockerRunModel)(nil)

type (
	// DockerRunModel is an interface to be customized, add more methods here,
	// and implement the added methods in customDockerRunModel.
	DockerRunModel interface {
		dockerRunModel
		FindAll(ctx context.Context) ([]DockerRun, error)
	}

	customDockerRunModel struct {
		*defaultDockerRunModel
	}
)

func (c customDockerRunModel) FindAll(ctx context.Context) ([]DockerRun, error) {
	var data []DockerRun

	err := c.conn.Find(ctx, &data, bson.M{})
	switch err {
	case nil:
		return data, nil
	case mon.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

// NewDockerRunModel returns a model for the mongo.
func NewDockerRunModel(url, db, collection string) DockerRunModel {
	conn := mon.MustNewModel(url, db, collection)
	return &customDockerRunModel{
		defaultDockerRunModel: newDefaultDockerRunModel(conn),
	}
}
